package com.shinewind.info.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shinewind.info.mapper.IUserInfoMapper;
import com.shinewind.info.mapper.IUserMapper;
import com.shinewind.info.service.IUserDetailService;
import com.shinewind.info.service.IUserInfoService;
import com.shinewind.pojo.dto.MyUserDto;
import com.shinewind.pojo.dto.UserDto;
import com.shinewind.pojo.entity.UserDetail;
import com.shinewind.pojo.entity.UserInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
@DubboService
public class UserInfoServiceImpl extends ServiceImpl<IUserMapper, UserInfo> implements IUserInfoService {
    @Autowired
    private IUserDetailService iUserDetailService;

    @Override
    public UserInfo getUserById(String uId) {
        UserInfo userInfo = this.lambdaQuery().eq(UserInfo::getUId,uId).one();
        return userInfo;
    }

    @Override
    public UserInfo getUserInfoByUsername(String username) {
        UserInfo userInfo = this.lambdaQuery().eq(UserInfo::getUserName,username).one();
        return userInfo;
    }

    @Override
    public UserInfo getUserInfoByNickname(String nickname) {
        UserInfo userInfo = this.lambdaQuery().eq(UserInfo::getNickName,nickname).one();
        return userInfo;
    }

    @Override
    public UserInfo getUserInfoByTelephone(String telephone) {
        UserInfo userInfo = this.lambdaQuery().eq(UserInfo::getTelePhone,telephone).one();
        return userInfo;
    }

    @Override
    public UserInfo insertANDGetUid(UserInfo userInfo) {
        Boolean flag = this.save(userInfo);

        if(flag == true){
            System.out.println("userInfo:"+userInfo.getUId());
            return userInfo;
        }
        else{
            return null;
        }
    }

    @Override
    public String updateUserInfoByDto(String uId, UserDto userDto) {
        UserInfo userInfo = this.lambdaQuery().eq(UserInfo::getUId,uId).one();
        UserDetail userDetail = iUserDetailService.lambdaQuery().eq(UserDetail::getUId,uId).one();
        //如果没有修改以前的信息
        if(userInfo.getNickName().equals(userDto.getNickName()) && userDetail.getSign().equals(userDto.getSign())){
            return "未修改个人资料";
        }
        //修改个人信息
        else{
            //两者都不同
            if(!userInfo.getNickName().equals(userDto.getNickName()) && !userDetail.getSign().equals(userDto.getSign())){
                //修改昵称
                Boolean flag1 = this.lambdaUpdate().eq(UserInfo::getUId,uId)
                        .set(UserInfo::getNickName,userDto.getNickName()).update();
                //修改签名
                Boolean flag2 = iUserDetailService.lambdaUpdate().eq(UserDetail::getUId,uId)
                        .set(UserDetail::getSign,userDto.getSign()).update();
                if(flag1 && flag2){
                    return "修改个人资料成功";
                }
                else{
                    return "修改个人资料失败";
                }
            }
            //昵称和以前不同
            else if(!userInfo.getNickName().equals(userDto.getNickName())){
                //修改昵称
                Boolean flag1 = this.lambdaUpdate().eq(UserInfo::getUId,uId)
                        .set(UserInfo::getNickName,userDto.getNickName()).update();
                if(flag1){
                    return "修改个人资料成功";
                }
                else{
                    return "修改个人资料失败";
                }
            }
            //签名和以前不同
            else if(!userDetail.getSign().equals(userDto.getSign())){
                //修改签名
                Boolean flag2 = iUserDetailService.lambdaUpdate().eq(UserDetail::getUId,uId)
                        .set(UserDetail::getSign,userDto.getSign()).update();
                if(flag2){
                    return "修改个人资料成功";
                }
                else{
                    return "修改个人资料失败";
                }
            }
        }
        return "未知错误";
    }

    @Override
    public Boolean updateUserInfo(UserInfo saveUserInfo) {
        Boolean flag = this.updateById(saveUserInfo);
        return flag;
    }
}
